var $window = $(window), gardenCtx, gardenCanvas, $garden, garden;
var clientWidth = $(window).width();
var clientHeight = $(window).height();

$(function(){
    $loveHeart = $("#loveHeart");
    $garden = $("#garden");
    gardenCanvas = $garden[0];
    

    $("#content").css("width",$loveHeart.width());
    $("#content").css("height",Math.max($loveHeart.height(),$loveHeart.width()));
    $("#content").css("margin-left",Math.max(($window.width()-$("#content").width())/2,10));
    
    gardenCanvas.width = $("#loveHeart").width();
    gardenCanvas.height = $("#content").height();
    gardenCtx = gardenCanvas.getContext("2d");
    gardenCtx.globalCompositeOperation = "lighter";
    garden = new Garden(gardenCtx, gardenCanvas);
    
    setInterval(function() {
        garden.render()
    }, Garden.options.growSpeed)
});
    
$(window).resize(function() {
    var b = $(window).width();
    var a = $(window).height();

    if(b != clientWidth && a != clientHeight) {
        location.replace(location)
    }
});

function getHeartPoint(c) {
    var b = c / Math.PI;
    var a = 19.5 * (16 * Math.pow(Math.sin(b), 3));
    var d =- 20 * (13 * Math.cos(b) - 5 * Math.cos(2 * b) - 2 * Math.cos(3 * b) - Math.cos(4 * b));
    return new Array(offsetX + a, offsetY + d);
}

function startHeartAnimation() {
    var c = 50;
    var d = 10;
    var b = new Array();
    var a = setInterval(function() {
        var h = getHeartPoint(d);
        var e = true;
        for(var f = 0; f < b.length; f++) {
            var g = b[f];
            var j = Math.sqrt(Math.pow(g[0] - h[0], 2) + Math.pow(g[1] - h[1], 2));
            if(j < Garden.options.bloomRadius.max * 1.3) {
                e = false;
                break;
            }
        }
        
        if(e) {
            b.push(h);
            garden.createRandomBloom(h[0], h[1])
        }
        
        if(d>=30) {
            clearInterval(a);
            showMessages()
        } else {
            d += 0.2
        }
    }, c)
}

(function(a) {
    a.fn.typewriter = function(doneCallback) {
        
        this.each(function() {
            var d = a(this), c=d.html(), b=0;
            d.html("");
            d.show();
            var e = setInterval(function() {
                var f = c.substr(b,1);
                if(f == "<") {
                    b = c.indexOf(">", b) + 1
                } else {
                    b++
                }
                console.log(c.substring(0,b) + (b & 1 ? "_" : ""))
                d.html(c.substring(0,b) + (b & 1 ? "_" : ""));
                
                if(b >= c.length) {
                    clearInterval(e)
                    d.fadeOut(3000, function() {
                        if (typeof(doneCallback) == 'function') {
                            doneCallback();
                        }
                    })
                }
            }, 75)
        });

        return this
    }
})(jQuery);

function timeElapse(c) {
    var e = Date();
    var f = (Date.parse(e) - Date.parse(c)) / 1000;
    var g = Math.floor( f / (3600 * 24));

    f = f % (3600 * 24);
    var b = Math.floor(f / 3600);

    if(b < 10) {
        b = "0" + b
    }
    
    f = f % 3600; 
    var d = Math.floor(f / 60);
    if(d < 10) {
        d = "0" + d
    }
    
    f = f % 60;
    
    if(f < 10) {
        f = "0" + f
    }
    
    var a = '<span class="digit">' + g + '</span>&nbsp;&nbsp;Days&nbsp;&nbsp; <br><span class="digit">&nbsp;&nbsp;' + b + '</span>&nbsp;&nbsp;Hours&nbsp;<br> <span class="digit">&nbsp;&nbsp;' + d + '</span>&nbsp;&nbsp;Minute<br> <span class="digit">&nbsp;&nbsp;' + f + "</span>&nbsp;&nbsp;Second";
    
    $("#elapseClock").html(a)
}

function showMessages() {
    adjustWordsPosition();
    $("#messages").fadeIn(5000, function() {
        showLoveU()
    })
}

function adjustWordsPosition() {
    $("#words").css("position", "absolute");
    $("#words").css("top", $("#garden").position().top + 165);
    $("#words").css("left", $("#garden").position().left + ($("#garden").width() - $("#words").width()) / 2)
}

function adjustCodePosition() {
    //$("#code").css("margin-top",($("#garden").height()-$("#code").height())/2)
}

function showLoveU() {
    $("#loveu").fadeIn(3000)

    window.setTimeout(function() {
        $("#loveHeart").fadeOut(3000, function(){
            setInterval(draw, 50)
        });
    }, 10000);
};